package com.neusoft.data.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.neusoft.courseSelect.domain.PieData;
import com.neusoft.data.domain.SerChatHistory;
import com.neusoft.data.domain.SerServiceEvaluation;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neusoft.data.mapper.SerScoreMapper;
import com.neusoft.data.domain.SerScore;
import com.neusoft.data.service.ISerScoreService;

/**
 * 课程成绩Service业务层处理
 * 
 * @author slh
 * @date 2023-12-31
 */
@Service
public class SerScoreServiceImpl implements ISerScoreService
{
    @Autowired
    private SerScoreMapper serScoreMapper;

    /**
     * 查询课程成绩
     *
     * @param scoreId 课程成绩主键
     * @return 课程成绩
     */
    @Override
    public SerScore selectSerScoreByScoreId(Long scoreId)
    {
        return serScoreMapper.selectSerScoreByScoreId(scoreId);
    }

    /**
     * 查询课程成绩列表
     *
     * @param serScore 课程成绩
     * @return 课程成绩
     */
    @Override
    public List<SerScore> selectSerScoreList(SerScore serScore)
    {
        return serScoreMapper.selectSerScoreList(serScore);
    }

    /**
     * 新增课程成绩
     *
     * @param serScore 课程成绩
     * @return 结果
     */
    @Override
    public int insertSerScore(SerScore serScore)
    {
        serScore.setCreateTime(DateUtils.getNowDate());
        return serScoreMapper.insertSerScore(serScore);
    }

    /**
     * 修改课程成绩
     *
     * @param serScore 课程成绩
     * @return 结果
     */
    @Override
    public int updateSerScore(SerScore serScore)
    {
        serScore.setUpdateTime(DateUtils.getNowDate());
        return serScoreMapper.updateSerScore(serScore);
    }

    /**
     * 批量删除课程成绩
     *
     * @param scoreIds 需要删除的课程成绩主键
     * @return 结果
     */
    @Override
    public int deleteSerScoreByScoreIds(Long[] scoreIds)
    {
        return serScoreMapper.deleteSerScoreByScoreIds(scoreIds);
    }

    /**
     * 删除课程成绩信息
     *
     * @param scoreId 课程成绩主键
     * @return 结果
     */
    @Override
    public int deleteSerScoreByScoreId(Long scoreId)
    {
        return serScoreMapper.deleteSerScoreByScoreId(scoreId);
    }

    @Override
    public String importUser(List<SerScore> list, boolean updateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0) {
            throw new ServiceException("导入成绩不存在");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (SerScore evaluation : list) {
            try {
                SerScore e = serScoreMapper.selectSerScoreByScoreId(evaluation.getScoreId());

                if (StringUtils.isNull(e)) {
                    serScoreMapper.insertSerScore(evaluation);
                    successNum ++;
                    successMsg.append("<br/>" + successNum + "、成绩 " + evaluation.getScoreId() + " 导入成功：");
                } else if (updateSupport) {
                    serScoreMapper.insertSerScore(evaluation);
                    successMsg.append("<br/>" + successNum + "、成绩 " + evaluation.getScoreId() + " 更新成功：");
                } else {
                    failureNum ++;
                    failureMsg.append("<br/>" + failureNum + "、成绩 " + evaluation.getScoreId() + " 已存在：");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、成绩 " + evaluation.getScoreId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败！共 " + failureNum + " 条数据格式不正确，错误如下");
            throw  new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "导入成功共 " + successNum + " 条数据");
        }

        return successMsg.toString();
    }

    // 获取成绩分布情况
    @Override
    public List<Integer> getBarData(SerScore serScore) {
        List<Integer> data = new ArrayList<>();
        List<SerScore> list = serScoreMapper.selectSerScoreList(serScore);

        int a = 0;
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        for (SerScore score : list) {
            if (score.getStuScore() != null) {
                if (score.getStuScore() >= 0 && score.getStuScore() <= 20) {
                    a++;
                } else if (score.getStuScore() <= 40) {
                    b++;
                } else if (score.getStuScore() <= 60) {
                    c++;
                } else if (score.getStuScore() <= 80) {
                    d++;
                } else {
                    e++;
                }
            }
        }

        data.add(a);
        data.add(b);
        data.add(c);
        data.add(d);
        data.add(e);

        return data;
    }

    @Override
    public List<PieData> getPieData(SerScore serScore) {
        List<PieData> data = new ArrayList<>();
        List<SerScore> list = serScoreMapper.selectSerScoreList(serScore);

        PieData aa = new PieData();
        PieData bb = new PieData();
        PieData cc = new PieData();
        aa.setName("通过");
        bb.setName("重修");
        cc.setName("补考");

        int a = 0;
        int b = 0;
        int c = 0;

        for (SerScore score : list) {
            if (score.getGetWay() != null) {
                if (score.getGetWay().equals("初修")) {
                    a++;
                } else if (score.getGetWay().equals("重修")) {
                    b++;
                } else {
                    c++;
                }
            }
        }

        aa.setValue(a);
        bb.setValue(b);
        cc.setValue(c);
        data.add(aa);
        data.add(bb);
        data.add(cc);

        return data;
    }

    @Override
    public List<String> getYears() {

        List<SerScore> list = serScoreMapper.selectSerScoreList(new SerScore());
        List<String> years = list.stream()
                .map(SerScore::getGetYear)
                .distinct()
                .collect(Collectors.toList());

        return years;
    }

    @Override
    public List<String> getNames() {

        List<SerScore> list = serScoreMapper.selectSerScoreList(new SerScore());
        List<String> names = list.stream()
                .map(SerScore::getCourseName)
                .distinct()
                .collect(Collectors.toList());

        return names;
    }
}
